package com.wwwc.index.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;

import com.wwwc.util.web.*;

public class WebTextServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
    {
	response.setContentType("text/html");

	String user_ip       = request.getRemoteAddr();
	HttpSession session  = request.getSession(true);
        PrintWriter out      = response.getWriter();
	/*
	ResourceBundle SystemInfo = (ResourceBundle)session.getAttribute("SystemInfo");
	
	if (SystemInfo == null) {
	    Locale locale = request.getLocale();
	    SystemInfo = ResourceBundle.getBundle("SystemInfo", locale); 
	    session.setAttribute("SystemInfo", SystemInfo);
	}

	String action = request.getParameter("action");
	String url    = request.getParameter("url");

	if(action == null) {
	    out.println("<HTML><BODY><CENTER>");
	    out.println("<H1>Please Select Your Options</H1>");
	    out.println("<BR><BR>");
	    out.println("<TABLE border=0  cellSpacing=0 cellPadding=0 style='font-size: 10pt'>");
	    out.println("<TR><TD align=right>");
	    out.println("<FORM action=WebTextServlet>");
	    out.println("<input type=submit name=action value='Update Datek SID' style='font-size: 8pt'>");
	    out.println("<input type=hidden name=url value=datek.url.sid style='font-size: 8pt'>");
	    out.println("</FORM>");
	    out.println("</TD><TD align=left>");
	    out.println("<FORM action=WebTextServlet>");
	    out.println("<input type=submit name=action value=Other style='font-size: 8pt'>");
	    out.println("</FORM>");
	    out.println("</TD></TR>");
	    out.println("</TABLE>");
	    out.println("</BODY></HTML>");
	    return;
	}

	if(action.equals("Update Datek SID")) {
	    url = SystemInfo.getString(url);
	    out.println("<HTML><BODY><CENTER>");
	    out.println("<FORM method=post action=WebTextServlet>");
	    out.println("<H2>Please Select Data Range</H2>");
	    out.println("<TABLE border=1  cellSpacing=0 cellPadding=0 style='font-size: 10pt' width=100%>");
	    out.println("<TR><TD><TEXTAREA name=url rows=20 cols=100>");
	    out.println(url);
	    out.println("</TEXTAREA></TD></TR>");
	    out.println("<TR><TD align=center hight=50>");
	    out.println("<input type=submit value='Load Data' style='font-size: 8pt'>");
	    out.println("<input type=hidden name=action value=load_data style='font-size: 8pt'>");
	    out.println("</TD></TR>");
	    out.println("</FORM>");
	    out.println("</TABLE>");
	    out.println("</BODY></HTML>");
	    response.flushBuffer();
	    return;
	}

	if(action.equals("load_data")) {
	    url = request.getParameter("url");
	    String start_str =  SystemInfo.getString("datek.url.string.start");
	    String end_str   =  SystemInfo.getString("datek.url.string.end");
	    String temp = null;

	    if(url.startsWith("https://")) {
		MyHttpsConnection cn = new MyHttpsConnection();
		cn.connectToUrl(url);
		out.println("<HTML><BODY><CENTER>");
		out.println("<FORM method=post action=WebTextServlet>");
		out.println("<TABLE border=1  cellSpacing=0 cellPadding=0 style='font-size: 10pt' width=100%>");
		out.println("<TR><TD><TEXTAREA name=input_str rows=20 cols=100>");
		
		Vector v = cn.getReturnLineVector();
		Enumeration enum = v.elements();
		int total = 0;
		while (enum.hasMoreElements()) {
		    temp = (String)enum.nextElement();
		    if(temp != null && temp.startsWith(start_str) && temp.endsWith(end_str)) {
			temp = temp.substring(temp.indexOf("symb="));
			temp = temp.replaceAll(end_str, "");
			if(!temp.startsWith("symb=1") && !temp.startsWith("symb=6") && 
			   !temp.startsWith("symb=2") && !temp.startsWith("symb=7") && 
			   !temp.startsWith("symb=3") && !temp.startsWith("symb=8") && 
			   !temp.startsWith("symb=4") && !temp.startsWith("symb=9") && 
			   !temp.startsWith("symb=5") && !temp.startsWith("symb=0")) {
			    out.println(temp+";");
			    total++;
			}
		    }
		}
		
		out.println("</TEXTAREA></TD></TR>");
		out.println("<TR><TD align=centee>Total Record:"+total+"</TD></TR>");
	    }
	  
	    out.println("<TR><TD align=center>");
	    StringTokenizer tokens = new StringTokenizer(databases, ",");

	    while (tokens.hasMoreTokens()) {
		temp = tokens.nextToken();
		out.println("<input type=checkbox name="+temp+" value="+temp+">"+temp+" &nbsp;");
	    }

	    out.println("</TD></TR>");

	    out.println("<TR><TD align=center hight=50>");
	    out.println("<input type=submit value='Update Database' style='font-size: 8pt'>");
	    out.println("<input type=hidden name=action value='update_sid' style='font-size: 8pt'>");
	    out.println("</TD></TR>");
	    out.println("</FORM>");
	    out.println("</TABLE>");
	    out.println("</BODY></HTML>");
	    response.flushBuffer();
	    return;
	}

	if(action.equals("update_sid")) {
	    String input_str = request.getParameter("input_str");
	    String temp  = null;
	    String sdb   = null;

	    out.println("<HTML><HEAD><TITLE></TITLE></HEAD><BODY><CENTER>");
	    out.println("<H1>Excution Result</H1>");
	    
	    out.println("<TABLE border=1 style='font-size: 10pt' width=90%>");
	    
	    StringTokenizer tokens_db = new StringTokenizer(databases, ","); 	
	  
	    while (tokens_db.hasMoreTokens()) {
		int total =0;
		long stime = System.currentTimeMillis();
		temp = tokens_db.nextToken();
		sdb  = request.getParameter(temp);
		if(sdb != null && sdb.equals(temp)) {
		    Database db = DatabaseAdapter.getDatabase(sdb, SystemInfo);
		    String query = null;
		    String osid  = null;
		    StringTokenizer tokens_query = new StringTokenizer(input_str, ";");
		    String tick = null;
		    String sid  = null;
		    while (tokens_query.hasMoreTokens()) {
			temp = tokens_query.nextToken();
			if(temp != null && (temp.trim()).startsWith("symb=")) {
			    tick  = temp.substring(   temp.indexOf("symb=")+5,   temp.indexOf("&sid") );
			    sid   = temp.substring(temp.indexOf("&sid=")+5);
			    query = "SELECT * FROM STOCK_SID WHERE TICK='"+tick+"' AND URL='datek';";
			    osid  = db.getSingleFieldValue(query,"SID");
			    if(osid != null && osid.length()>1) {
				if(!osid.equals(sid)) {
				    total++;
				    query = "UPDATE STOCK_SID SET SID='"+sid+"' WHERE TICK='"+tick+"' AND URL='datek';";
				    out.println("<TR><TD>Database</TD><TD>"+sdb+"</TD><TD>Update</TD>");
				    out.println("<TD>Result</TD><TD>"+db.update(query)+"</TD></TR>");
				    out.println("<TR><TD>Query</TD><TD colspan=4>"+query+"</TD></TR>");
				}
			    }
			    else {
				total++;
				query = "INSERT INTO STOCK_SID (TICK, SID, URL) VALUES('"+tick+"','"+sid+"', 'datek');";
				out.println("<TR><TD>Database</TD><TD>"+sdb+"</TD><TD>Insert</TD>");
				out.println("<TD>Result</TD><TD>"+db.insert(query)+"</TD></TR>");
				out.println("<TR><TD>Query</TD><TD colspan=4>"+query+"</TD></TR>");
			    }
			}
			else {
			    if(temp != null && temp.length()>1) {
				out.println("<BR>Junk Found="+temp);
			    }
			}		
		    }
		}
		out.println("<BR>Database["+sdb+"]");
		out.println("<BR>Total Record["+total+"]");
		out.println("<BR>Total Time["+(System.currentTimeMillis()-stime)+"]");
	    }
	  
	    out.println("</TABLE>");
	    out.println("</BODY></HTML>");
	    return;
	}

	if(action.equals("Other")) {
	    out.println("<HTML><BODY><CENTER>");
	    out.println("<H1>Please Enter The URL</H1>");
	    out.println("<BR><BR>");
	    out.println("<TABLE border=1  cellSpacing=0 cellPadding=0 style='font-size: 10pt'>");
	    out.println("<FORM action=WebTextServlet>");
	    out.println("<TR><TD align=left><input type=text name=url value=http:// size=100></TD></TR>");
	    out.println("<TR><TD colspan=2 align=center height=50>");
	    out.println("<input type=submit name=action value='Original Page' style='font-size: 8pt'>");
	    out.println("<input type=submit name=action value='Line By Line' style='font-size: 8pt'>");
	    out.println("<input type=submit name=action value='Set Range' style='font-size: 8pt'>");
	    out.println("</TD></TR>");
	    out.println("</FORM>");
	    out.println("</TABLE>");
	    out.println("</BODY></HTML>");
	    return;
	}

	if(action.equals("Original Page") || action.equals("Line By Line")) {
	    if(url.startsWith("https://")) {
		MyHttpsConnection cn = new MyHttpsConnection();
		cn.connectToUrl(url);
		out.println("<HTML><BODY><CENTER>");
		out.println("<TABLE border=1  cellSpacing=0 cellPadding=0 style='font-size: 10pt' width=100%>");
		if(action.equals("Original Page")) {
		    String temp = (cn.getFullReturnStringBuffer()).toString();
		    if(temp != null && temp.indexOf("<") != -1) {
			temp = temp.replaceAll("<", "&lt");
		    }
		    if(temp != null && temp.indexOf(">") != -1) {
			temp = temp.replaceAll(">", "&gt");
		    }
		    out.println("<TR><TD>");
		    out.println(temp);
		    out.println("</TD></TR>");
		}
		if(action.equals("Line By Line")) {
		    String temp = null;
		    out.println("<TR><TD>");
		    Vector v = cn.getReturnLineVector();
		    Enumeration enum = v.elements();
		    while (enum.hasMoreElements()) {
			temp = (String)enum.nextElement();
			if(temp != null && temp.indexOf("<") != -1) {
			    temp = temp.replaceAll("<", "&lt");
			}
			if(temp != null && temp.indexOf(">") != -1) {
			    temp = temp.replaceAll(">", "&gt");
			}
			out.println("<BR>"+temp);
		    }
		    out.println("</TD></TR>");
		}

		out.println("</TABLE>");
		out.println("</BODY></HTML>");
	    }

	    if(url.startsWith("http://")) {
		MyHttpConnection cn = new MyHttpConnection();
		out.println("<HTML><BODY><CENTER>");
		out.println("<TABLE border=1  cellSpacing=0 cellPadding=0 style='font-size: 10pt' width=100%>");
		
		if(cn.connectToUrl(url)) {
		    if(action.equals("Original Page")) {
			String temp = (cn.getFullReturnStringBuffer()).toString();
			out.println("<TR><TD>");
			out.println("<TR><TD><TEXTAREA name=query rows=30 cols=74>"+temp+"</TEXTAREA></TD></TR>");
			out.println("</TD></TR>");
		    }

		    if(action.equals("Line By Line")) {
			String temp = null;
			out.println("<TR><TD>");
			Vector v = cn.getReturnLineVector();
			Enumeration enum = v.elements();
			while (enum.hasMoreElements()) {
			    temp = (String)enum.nextElement();
			    if(temp != null && temp.indexOf("<") != -1) {
				temp = temp.replaceAll("<", "&lt");
			    }
			    if(temp != null && temp.indexOf(">") != -1) {
				temp = temp.replaceAll(">", "&gt");
			    }
			    out.println("<BR>"+temp);
			}
			out.println("</TD></TR>");
		    }
		}
		else {
		    out.println(cn.getMessage());
		}
		out.println("</TABLE>");
		out.println("</BODY></HTML>");
	    }
	    return;
	    }
	*/
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
    {
	doPost(request, response);
    }
}








